//声明js
var time = require('../../utils/util.js')
var formutils = require('../../utils/http.js');
var app = getApp();
Page({
  data: {
    _tabIndex: 2,
    //全局变量
    list: [],
    //加载样式是否显示
    loading: true,
    projectindex: 0,
    project: [],
    xuanzeindex: 0,
    xuanze: [],
    industry_id: "",
    project_id: "",
    source_id: "",
    user_id: "",
    searchKey: "",
    city2:'',
    mianji_id:'',
    yewuyuan: "",
    area:[],                            
    areaIndex:0,
    mianji:[],
    mianjiIndex:0,
    yewuyuanindex: 0,
    type: '',//默认时间类型:yearyesday,week,month,all
    page: 1,
    isSearch: false,
    listVisable: false,
    isCustomerManger: false, //客户管理
  },

  onLoad: function (options) {
    if (options.type == 1) {
      this.setData({
        type: "yesterday"
      })
    } else if (options.type == 2) {
      this.setData({
        type: "week"
      })
    } else if (options.type == 3) {
      this.setData({
        type: "month"
      })
    } else {
      this.setData({
        type: ""
      })
    }
    if (options.project_id != undefined && options.project_id != null) {
      this.setData({
        project_id: options.project_id
      })
    }

    //获取行业数据
    var that = this;
    wx.request({
      url: app.globalData.httpurl + 'industry',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        project_id: options.project_id
      },
      success: function (res) {
        var adds = [{ "industry_id": "", "name": "行业" }];
        that.setData({
          project: adds.concat(res.data.data)
        })
      }
    })
    //获取区域
    var that = this;
    wx.request({
      url: app.globalData.httpurl + 'customer/area',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
       
      },
      success: function (res) {
        var adds = [{ "city_id": "", "name": "区域" }];
        that.setData({
          area: adds.concat(res.data.data)
        })
      }
    })
    //获取面积
    var that = this;
    wx.request({
      url: app.globalData.httpurl + 'customer/mianji',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var adds = [{ "mianji_id": "", "name": "面积" }];
        that.setData({
          mianji: adds.concat(res.data.data)
        })
      }
    })
    //筛选人员
    wx.request({
      url: app.globalData.httpurl + 'user/list1',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      method: "GET",
      success: res => {
        var datas = res.data.data; //res.data就是从后台接收到的值

        var adds = [{ "user_id": "", "name": "业务员" }];

        this.setData({
          yewuyuan: adds.concat(res.data.data)
        })




      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

    //获取项目
    wx.request({
      url: app.globalData.httpurl + 'v2/project',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var adds = [{ "project_id": "", "name": "请选择领取到哪个项目" }];
        that.setData({
          xuanze: adds.concat(res.data.data)
        })
        for (var i = 0; i < that.data.xuanze.length; i++) {
          if (that.data.xuanze[i].project_id == options.project_id) {
            that.setData({
              xuanzeindex: i
            })
          }
        }
      }
    })

    wx.startPullDownRefresh();

  },
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1,
    })
    this.LoadData();
  },
  bindshaixuan: function (e) {
    this.setData({
      projectindex: e.detail.value,
      industry_id: this.data.project[e.detail.value].industry_id,
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  bindshaixuan2: function (e) {
    console.log(e)
    if (e.detail.value=='0'){
      wx.showToast({
        title: '请选择一个项目',
        icon:'none'
      })
      return
    }
    let project_id = this.data.xuanze[e.detail.value].project_id
   wx.request({
     url: app.globalData.httpurl + 'customer/receive',
      
     data: {
       customer_id:e.currentTarget.dataset.id,
       project_id
     },
     header: {
       "Authorization": "bearer " + app.globalData.access_token
     },
     method: 'POST',
     dataType: 'json',
     responseType: 'text',
     success: res=> {
        if(res.data.code==0){
            let list= this.data.list
            list.splice(list.findIndex(function(item){
              return item.customer_id == e.currentTarget.dataset.id
            }),1)
            this.setData({
              list
            })
            wx.showToast({
              title: res.data.msg,
              icon:'success'
            })
        }else{
          wx.showToast({
            title: res.data.msg,
          })
        }
        

     },
     fail: function(res) {
       wx.showToast({
         title: '网络出错',
         icon: 'none',
     
         success: function(res) {},
         fail: function(res) {},
         complete: function(res) {},
       })
     },
     complete: function(res) {},
   })
  },
  bindshaixuan3: function (e) {
    this.setData({
      yewuyuanindex: e.detail.value,
      user_id: this.data.yewuyuan[e.detail.value].user_id,
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  bindsousuo: function () {
    this.setData({
      isSearch: true,
      listVisable: true,
    })
  },
  bindArea:function(e){
    this.setData({
        areaIndex:e.detail.value,
        city2: this.data.area[e.detail.value].city_id,
        list: [],
        page: 1,
    })
    this.LoadData();
  },
  bindmianji(e){
    this.setData({
      mianjiIndex: e.detail.value,
      mianji_id: this.data.mianji[e.detail.value].mianji_id,
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  RemoveCustomer:function(customer_id){
    console.log('返回页面的customer_id', customer_id)
    let list = this.data.list
    list.splice(list.findIndex(function (item) {
      return item.customer_id == customer_id
    }), 1)
    this.setData({
      list
    })
    wx.showToast({
      title: '领取',
      icon: 'success'
    })
  },
  LoadData: function () {
    var that = this //很重要，一定要写
    wx.request({
      url: app.globalData.httpurl + 'seaCoustomer',
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "industry_id": this.data.industry_id,
        "project_id": this.data.project_id,
        "source_id": this.data.source_id,
        "page": this.data.page,
        "search_key": this.data.searchKey,
        "time": this.data.type,
        "user_id": this.data.user_id,
        city2:this.data.city2,
        mianji_id: this.data.mianji_id
      },
      method: "GET",
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        console.log(datas)
        for (var i = 0; i < datas.length; i++) {
          datas[i]["created_at"] = datas[i]["created_at"].substring(5, 10)
        }
        that.setData({ //循环完后，再对list进行赋值
          list: that.data.list.concat(datas),
          loading: false
        })
        wx.stopPullDownRefresh();
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

    wx.request({
      url: app.globalData.httpurl + 'industry',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        project_id: that.data.project_id
      },
      success: function (res) {
        var adds = [{ "industry_id": "", "name": "行业" }];
        that.setData({
          project: adds.concat(res.data.data)
        })
      }
    })
  },
  bindPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.id,
    })
  },
  bindfenpei: function (e) {
    wx.navigateTo({
      // url: '/pages/persionlist/persionlist?id=' + e.currentTarget.dataset.id,
      url: '/pages/persionlist/persionlist?id=' + e.currentTarget.dataset.id,
    })
  },
  bindfenpei2: function (e) {
    wx.showToast({
      title: '该客户已分配过',
      icon: 'none'
    })
  },
  bindBtn: function () {
  },
  formhome: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)

    //权限获取完毕，跳转首页
    if (app.globalData.isHomeXiaoFei) {
      wx.reLaunch({
        url: '/pages/home3/home3',
      })
    } else {
      wx.reLaunch({
        url: '/pages/home2/home2',
      })
    }
  },
  formkehu: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    if (app.globalData.isCustomerDistribution) {
      wx.navigateTo({
        url: '/pages/fenpeilist/fenpeilist',
      })
    } else {
      wx.navigateTo({
        url: '/pages/list/listCustom',
      })
    }
  },
  formadd: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    wx.navigateTo({
      url: '/pages/menu/menus',
    })
  },
  bindkey: function (e) {
    this.setData({
      searchKey: e.detail.value,
      list: [],
      page: 1,
      listVisable: false,
    })
    this.LoadData()
  },
  formcancel: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    this.setData({
      isSearch: false,
      listVisable: false,
    })
  },
})